<?php
include('../global.php');

//VS
if(isset($_POST['left_id'])&&isset($_POST['right_id'])){
	$left_id = $_POST['left_id'];
	$right_id = $_POST['right_id'];
	$left_score = $_POST['left_score'];
	$right_score = $_POST['right_score'];
	$awin =$_POST['awin'];
	
	$score = $db->eloRank($left_score,$right_score,$awin);
	if($awin == 1){
		$sql = "UPDATE `renren_hunauhomeurl` SET `votes` = `votes` + 1,`rank_score` = '$score[0]' WHERE `userid` = $left_id";
		$query = $db->query($sql);
		$sql = "UPDATE `renren_hunauhomeurl` SET `rank_score` = '$score[1]' WHERE `userid` = $right_id";
		$query = $db->query($sql);
	}else{
		$sql = "UPDATE `renren_hunauhomeurl` SET `rank_score` = '$score[0]' WHERE `userid` = $left_id";
		$query = $db->query($sql);
		$sql = "UPDATE `renren_hunauhomeurl` SET `votes` = `votes` + 1,`rank_score` = '$score[1]' WHERE `userid` = $right_id";
		$query = $db->query($sql);
	}
	
	$sql = "SELECT `userid`,`avatar`,`rank_score` FROM `renren_hunauhomeurl` WHERE `status` = 1 ORDER BY rand() LIMIT 2";
	$query = $db->query($sql);
	while($row = $db->fetch_array($query)){$data[] = array('userid'=>$row['userid'],'avatar'=>$row['avatar'],'rank_score'=>$row['rank_score']);}
	echo $data[0]['userid'].'|'.$data[0]['avatar'].'|'.$data[0]['rank_score'].'|'.$data[1]['userid'].'|'.$data[1]['avatar'].'|'.$data[1]['rank_score'];
}

//DRAW
if(isset($_POST['status'])){
	$sql = "SELECT `userid`,`avatar`,`rank_score` FROM `renren_hunauhomeurl` WHERE `status` = 1 ORDER BY rand() LIMIT 2";
	$query = $db->query($sql);
	while($row = $db->fetch_array($query)){$data[] = array('userid'=>$row['userid'],'avatar'=>$row['avatar'],'rank_score'=>$row['rank_score']);}
	echo $data[0]['userid'].'|'.$data[0]['avatar'].'|'.$data[0]['rank_score'].'|'.$data[1]['userid'].'|'.$data[1]['avatar'].'|'.$data[1]['rank_score'];
}

if(isset($_POST['report_id'])){
	$sql = "SELECT `userid` FROM `renren_hunauhomeurl` WHERE `userid` = '".$_POST['report_id']."'";
	$query = $db->query($sql);
	$num = $db->num_rows($query);
	if($num>0):
		$sql = "UPDATE `renren_hunauhomeurl` SET `fake_votes` = `fake_votes` + 1 WHERE `userid` = '".$_POST['report_id']."'";
		$query = $db->query($sql);
		echo 1;
	else:echo 0;
	endif;
}


?>